package com.spddr.pmos;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.List;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.utils.AnnotationUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.fasterxml.jackson.annotation.JsonIgnore;

import cn.seqdata.cxf.SpringConfigurationFactory;
import cn.seqdata.cxf.provider.GsonProvider;

@Configuration
public class CxfConfiguration extends SpringConfigurationFactory {

	@Override
	protected List<Object> getJaxrsProviders() {
		List<Object> providers = super.getJaxrsProviders();

		//特例，对打有@JsonIgnore标记的函数采用GsonProvider
		//只要用于处理echarts的Option，空值不输出到前台
		providers.add(0, new GsonProvider() {
			@Override
			public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
				return null != AnnotationUtils.getAnnotation(annotations, JsonIgnore.class)
		            && super.isReadable(type, genericType, annotations, mediaType);
			}

			@Override
			public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
				return null != AnnotationUtils.getAnnotation(annotations, JsonIgnore.class)
		            && super.isWriteable(type, genericType, annotations, mediaType);
			}
		});

		return providers;
	}

	@Override
	protected String getAddress() {
		return "/api";
	}

	@Override
	protected void setJaxrsResources(JAXRSServerFactoryBean sf) {
		//tree
		sf.setServiceClass(com.spddr.pmos.cxf.tree.impl.JaxrsPsrTreeImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.tree.impl.JaxrsOrgTreeImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.tree.impl.JaxrsTradeTreeImpl.class);

		//type
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsObjectTypeImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsPeriodTypeImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsRegionTypeImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsOrgTypeImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsTelemetryTypeImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsPlantTypeImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsRiskTypeImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsDispOrderTypeImpl.class);

		//region
//		sf.setServiceClass(com.spddr.pmos.cxf.impl.Jaxrs.class);

		//grid, company, customer,

		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsCompanyImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsGridImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsCustomerImpl.class);

		//plant, unit,
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsPlantImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsUnitImpl.class);

		//substation, line, transmission,
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsSubstationImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsLineImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsTransmissionImpl.class);

		//river, reservoir,
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsRiverImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsReservoirImpl.class);

		//usagePoint, usagePointGroup,
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsUsagePointImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsUsagePointGroupImpl.class);

		//monitorStation, hydrologyStation, rainStation, weatherStation,
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsHydrologyStationImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsRainStationImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsWeatherStationImpl.class);

		//program, trade, contract,
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsProgramImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsTradeImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsContractImpl.class);

		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsCustomerCreditImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsCustomerPerformImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsCustomerRiskImpl.class);

		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsReservoirLevelVolumeImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsUnitNhqImpl.class);
		sf.setServiceClass(com.spddr.pmos.cxf.impl.JaxrsUnitWaterCostImpl.class);
	}

	@Bean
	public Server jaxrsServer() {
		return super.createJaxRsServer();
	}
}
